/**
 * This file is part of aion-lightning <aion-lightning.org>.
 *
 * aion-lightning is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * aion-unique is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * aion-lightning. If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.clientpackets;

import com.aionengine.gameserver.configs.main.AutoGroupConfig;
import com.aionengine.gameserver.model.autogroup.EntryRequestType;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.AionClientPacket;
import com.aionengine.gameserver.network.aion.AionConnection.State;
import com.aionengine.gameserver.services.AutoGroupService;
import com.aionengine.gameserver.services.instance.DredgionService2;
import com.aionengine.gameserver.services.instance.KamarBattlefieldService;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author Shepper, Guapo, nrg
 */
public class CM_AUTO_GROUP extends AionClientPacket {

    private byte instanceMaskId;
    private byte windowId;
    private byte entryRequestId;

    public CM_AUTO_GROUP(int opcode, State state, State... restStates) {
        super(opcode, state, restStates);
    }

    @Override
    protected void readImpl() {
        instanceMaskId = (byte) readD();
        windowId = (byte) readC();
        entryRequestId = (byte) readC();
    }

    @Override
    protected void runImpl() {
        Player player = getConnection().getActivePlayer();
        if (!AutoGroupConfig.AUTO_GROUP_ENABLE) {
            PacketSendUtility.sendMessage(player, "Auto Group is disabled");
            return;
        }
        switch (windowId) {
            case 100:
                EntryRequestType ert = EntryRequestType.getTypeById(entryRequestId);
                if (ert == null) {
                    return;
                }
                AutoGroupService.getInstance().startLooking(player, instanceMaskId, ert);
                break;
            case 101:
                AutoGroupService.getInstance().unregisterLooking(player, instanceMaskId);
                break;
            case 102:
                AutoGroupService.getInstance().pressEnter(player, instanceMaskId);
                break;
            case 103:
                AutoGroupService.getInstance().cancelEnter(player, instanceMaskId);
                break;
            case 104:
                if (player.getLevel() < 61) {
                    DredgionService2.getInstance().showWindow(player, instanceMaskId);
                } else {
                    KamarBattlefieldService.getInstance().showWindow(player, instanceMaskId);
                }
                break;
            case 105:
                // DredgionRegService.getInstance().failedEnterDredgion(player);
                break;
        }
    }

}
